package com.example.spring_config.controller;

import com.example.spring_config.anno.PermissionVerAnno;
import com.example.spring_config.bean.LayuiTree;
import com.example.spring_config.bean.ResultInfo;
import com.example.spring_config.mapper.Permission;
import com.example.spring_config.mapper.User;
import com.example.spring_config.service.PermissionRoleRelationService;
import com.example.spring_config.service.PermissionService;
import com.example.spring_config.util.ToLayuiDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private PermissionRoleRelationService permissionRoleRelationService;

    /**
     * 添加权限
     * @param permission 权限对象
     * @return 添加状态对象
     */
    @PermissionVerAnno("addPermission")
    @RequestMapping(value = "/addPermission",method = RequestMethod.GET)
    public ResultInfo addPermission(Permission permission){
        //permission.setId(UUID.randomUUID().toString().replace("-","").toUpperCase());
        boolean insert = permissionService.save(permission);
        if(insert){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no();
        }
    }

    /**
     * 根据id获取权限数据
     * @param id 权限id
     * @return 结果对象
     */
    @PermissionVerAnno("getPermossionList")
    @RequestMapping("/getPermissionById")
    public ResultInfo getPermissionById(String id){
        Permission permission = permissionService.getById(id);
        if(permission==null){
            return ResultInfo.no("获取权限失败");
        }else {
            return ResultInfo.ok(permission);
        }
    }

    /**
     * 根据id修改权限
     * @param permission 权限对象
     * @return 结果对象
     */
    @PermissionVerAnno("updatePermissionById")
    @RequestMapping("/updatePermissionById")
    public ResultInfo updatePermissionById(Permission permission, HttpSession session){
        boolean b = permissionService.updateById(permission);

        // 获取所有的权限
        List<Permission> allPermission = permissionService.list();
        HashMap<String, Permission> allPermissionMap = new HashMap<>();
        allPermission.forEach(t->{
            allPermissionMap.put(t.getId(),t);
        });
        session.setAttribute("allPermissionMap",allPermissionMap);
        // 获取用户所有的权限
        User one = (User) session.getAttribute("login_user");
        Set<String> userPermission = permissionService.getUserPermissionUrlByRid(one.getId());
        // 把用户权限存入session
        session.setAttribute("userPermission",userPermission);

        if(b){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no();
        }
    }

    /**
     * 根据id删除节点
     * @param id 权限id
     * @return 结果对象
     */
    @PermissionVerAnno("deleteById")
    @RequestMapping(value = "/deleteById",method = RequestMethod.DELETE)
    public ResultInfo deleteById(String id){
        boolean b = permissionService.removeById(id);
        if(b){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no();
        }
    }

    /**
     * 获取权限集合
     * @return
     */
    @PermissionVerAnno("getPermossionList")
    @RequestMapping("/getPermossionList")
    public List getPermossionList(){
        List<Permission> permissions = permissionService.list();
        if(permissions==null){
            return Collections.emptyList();
        }else {
            return ToLayuiDataUtil.PermissionArrayToLayui(permissions,null);
        }
    }

    /**
     * 根据角色id获取权限
     * @param rid
     * @return
     */
    @PermissionVerAnno("getPermossionList")
    @RequestMapping("/getPermissionListByRid")
    public ResultInfo getPermissionListByRid(String rid){
        List<String> permissionIds = permissionRoleRelationService.getPermissionIds(rid);
        List<Permission> list = permissionService.list();
        List<LayuiTree> layuiTrees = ToLayuiDataUtil.PermissionArrayToLayui(list, permissionIds);
        if(layuiTrees==null || layuiTrees.size()==0){
            return ResultInfo.no("权限获取失败");
        }else {
            return ResultInfo.ok(layuiTrees);
        }
    }

    /**
     * 获取用户的菜单权限
     * @param uid
     * @return
     */
    @PermissionVerAnno("getPermossionList")
    @RequestMapping("/getUserPermission")
    public ResultInfo getUserPermission(String uid){
        Set<Permission> userPermission = permissionService.getUserPermission(uid);
        if(userPermission==null || userPermission.size()==0){
            return ResultInfo.no("未查询到权限");
        }else {
            return ResultInfo.ok(userPermission);
        }
    }

}
